package com.nfit.shopmodel.converter;


import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.StringUtils;

/**
 * ClassName:LongConverter
 * Package:com.nfit.shopweb.converter
 * Description
 *
 * @Author L
 * @Create 2024/12/11 18:49
 */
public class LongConverter implements Converter<Long> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Long.class;
    }

    @Override
    public Long convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 获取单元格的值，可能是字符串、数字等
        String cellValue = cellData.getStringValue();
        if (StringUtils.isEmpty(cellValue)) {
            return null; // 返回 null 或者设置为默认值
        }

        try {
            return Long.parseLong(cellValue); // 转换为 Long
        } catch (NumberFormatException e) {
            // 处理格式错误的情况
            return null; // 或者你可以抛出异常，或者返回默认值
        }
    }


    @Override
    public WriteCellData convertToExcelData(Long value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 将 Long 类型的数据转换为可以写入 Excel 的格式
        return new WriteCellData(value == null ? "" : String.valueOf(value)); // Excel 存储时的转换
    }
}
